package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzPagination;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.domain.RmpDictType;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.services.RmpDictTypeService;

@Controller
public class DictController {
	@Autowired
	private RmpDictTypeService rmpDictTypeService;
	@Autowired
	private OpLogService opLogService;

	@RequiresPermissions("dict:list")
	@RequestMapping(value = "/system/dict/list")
	public String list(HttpServletRequest request) {
		opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_DICT.toString());
		rmpDictTypeService.dwzFindAll((DwzPagination) request.getAttribute(DwzPagination.DWZPAGINATION_PROPERTY_KEY));
		return "system/dict/list";
	}

	@RequiresPermissions("dict:add")
	@RequestMapping(value = "/system/dict/add", method = RequestMethod.GET)
	public String toAdd() {
		return "system/dict/edit";
	}

	@RequiresPermissions("dict:add")
	@RequestMapping(value = "/system/dict/add", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse add(RmpDictType rmpDictType) {
		RmpDictType newRmpDictType = rmpDictTypeService.save(rmpDictType);
		opLogService.addLog(RmpOpLogTypeEnum.ADD.toString(), RmpOpLogBusiTypeEnum.RMP_DICT.toString(),
				newRmpDictType.getCode(), newRmpDictType.getName());
		return DwzResponseUtils.createSuccessResponse(true, "dict_panel");
	}

	@RequiresPermissions("dict:edit")
	@RequestMapping(value = "/system/dict/edit/{code}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("code") String dictTypeCode, ModelMap model) {
		model.put("dictType", rmpDictTypeService.get(dictTypeCode));
		return "system/dict/edit";
	}

	@RequiresPermissions("dict:edit")
	@RequestMapping(value = "/system/dict/edit", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse edit(RmpDictType rmpDictType) {
		opLogService.addLog("EDIT", "DICT", rmpDictType.getCode(), rmpDictType.getName());
		rmpDictTypeService.update(rmpDictType);

		opLogService.addLog(RmpOpLogTypeEnum.UPDATE.toString(), RmpOpLogBusiTypeEnum.RMP_DICT.toString(),
				rmpDictType.getCode(), rmpDictType.getName());
		return DwzResponseUtils.createSuccessResponse(true, "dict_panel");
	}

	@RequiresPermissions("dict:view")
	@RequestMapping(value = "/system/dict/view/{code}", method = RequestMethod.GET)
	public String toView(@PathVariable("code") String dictTypeCode, ModelMap model) {
		model.put("dictType", rmpDictTypeService.get(dictTypeCode));
		model.put("readOnly", "1");

		return "system/dict/edit";
	}

	@RequiresPermissions("dict:delete")
	@RequestMapping(value = "/system/dict/delete", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			List<String> idList = new ArrayList<String>();
			for (String id : ids.split(",")) {
				if (StringUtils.isNotBlank(id)) {
					idList.add(StringUtils.trim(id));
				}
			}
			rmpDictTypeService.delete(idList);
			for (String id : idList) {
				opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_DICT.toString(), id);
			}
		}
		return DwzResponseUtils.createSuccessResponse(false);
	}

	@RequestMapping(value = "/system/dict/validateCode")
	@ResponseBody
	public String validateCode(String code) {
		if (rmpDictTypeService.getIgnoreCase(code) == null) {
			return "1";
		} else {
			return "0";
		}
	}
}
